package cn.edu.cdut.service;

import cn.edu.cdut.dao.ScoreFileInfoDao;
import cn.edu.cdut.dao.StuFileInfoDao;
import cn.edu.cdut.dto.MyRes;
import cn.edu.cdut.model.ScoreFileInfo;
import cn.edu.cdut.model.StuFileInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Iterator;
import java.util.List;

/**
 * Author  kobe
 * Created on 2017-05-28-18:23
 */
@Service
public class UploadService {
        @Autowired
        private StuFileInfoDao stuFileInfoDao;

        @Autowired
        private ScoreFileInfoDao scoreFileInfoDao;

        // 这里只是简单例子，文件直接输出到项目路径下。
        // 实际项目中，文件需要输出到指定位置，需要在增加代码处理。"src/main/resources/uploadFile/"+
        // 还有关于文件格式限制、文件大小限制，详见：中配置。
        @Transactional
        public void uploadFlie(MultipartFile file) throws IOException {

                String fileName = file.getOriginalFilename();
                if (fileName.contains("名单")) {
                        List<StuFileInfo> stuFileInfos = stuFileInfoDao.findAll();
                        if (stuFileInfos.size() != 0 ) {
                                String filename1 = stuFileInfos.get(0).getStufileName();
                                File file1 = new File("src/main/resources/static/" + filename1);
                                if (file1.exists()) {
                                        if (file1.delete()) {
                                                System.out.println("删除单个文件" + filename1 + "成功！");
                                                stuFileInfoDao.deleteAll();
                                                StuFileInfo stuFileInfo = new StuFileInfo();
                                                stuFileInfo.setStufileName(fileName);
                                                stuFileInfoDao.save(stuFileInfo);
                                        }
                                }
                        } else {
                                StuFileInfo stuFileInfo = new StuFileInfo();
                                stuFileInfo.setStufileName(fileName);
                                stuFileInfoDao.save(stuFileInfo);
                        }
                } else if (fileName.contains("成绩")) {
                        List<ScoreFileInfo> scoreFileInfos = scoreFileInfoDao.findAll();
                        if (scoreFileInfos.size() != 0) {
                                String filename2 = scoreFileInfos.get(0).getScorefileName();
                                File file2 = new File("src/main/resources/static/" + filename2);
                                if (file2.exists()) {
                                        if (file2.delete()) {
                                                System.out.println("删除单个文件" + filename2 + "成功！");
                                                scoreFileInfoDao.deleteAll();
                                                ScoreFileInfo scoreFileInfo = new ScoreFileInfo();
                                                scoreFileInfo.setScorefileName(fileName);
                                                scoreFileInfoDao.save(scoreFileInfo);
                                        }
                                }
                        }else {
                                ScoreFileInfo scoreFileInfo = new ScoreFileInfo();
                                scoreFileInfo.setScorefileName(fileName);
                                scoreFileInfoDao.save(scoreFileInfo);
                        }
                }
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File("src/main/resources/static/"+fileName)));
                out.write(file.getBytes());
                out.flush();
                out.close();
        }

        @Transactional
        public MyRes getAllStuFileInfo() {

                Iterable<StuFileInfo> stuFileInfos = stuFileInfoDao.findAll();
                //转换为Json字符串
                ObjectMapper mapper = new ObjectMapper();
                Iterator iter = stuFileInfos.iterator();
                String[] resArr = new String[(int)stuFileInfoDao.count()];
                int i = 0;
                while(iter.hasNext()){
                        try {
                                resArr[i] = mapper.writeValueAsString(iter.next());
                                i++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(201, "获取名单文件信息失败！", null);
                        }
                }
                return new MyRes(200, null, resArr);
        }

        @Transactional
        public MyRes getAllScoreFileInfo() {

                Iterable<ScoreFileInfo> scoreFileInfos = scoreFileInfoDao.findAll();
                //转换为Json字符串
                ObjectMapper mapper = new ObjectMapper();
                Iterator iter = scoreFileInfos.iterator();
                String[] resArr = new String[(int)scoreFileInfoDao.count()];
                int i = 0;
                while(iter.hasNext()){
                        try {
                                resArr[i] = mapper.writeValueAsString(iter.next());
                                i++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(201, "获取成绩文件信息失败！", null);
                        }
                }
                return new MyRes(200, null, resArr);
        }
}
